//
//  ByFootBallBetVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/20.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByFootBallBetVC.h"
#import "ByFootBallTypeView.h"
#import "ByFootBallSecitionView.h"
#import "ByFootBallMixedBetCell.h"//混合投注
#import "ByFootBallSPFBetCell.h"//胜平负
#import "ByFootBallAllGoalsCell.h"
#import "ByFootBallScoreBetCell.h"
#import "ByFootBallHalfAllCell.h"
#import "ByFootBallBottomView.h"
#import "ByFootBallMixedPopView.h"
#import "ByFootBallListVC.h"
#import "ByFootBallScoreBetPopView.h"
#import "ByFootBallWayTimesView.h"

@interface ByFootBallBetVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *titleBtn;
@property (nonatomic, strong) ByFootBallTypeView *typeView;
@property (nonatomic, strong)UIButton  *   maskView;// 背景

@property (nonatomic, strong) ByFootBallBottomView *bottomView;
@property (nonatomic, assign) NSInteger numberOfMatch;

@end

@implementation ByFootBallBetVC

- (NSArray *)timesArray{
    if (_timesArray == nil) {
        _timesArray = [NSArray array];
    }
    return _timesArray;
}


- (ByFootBallTypeView *)typeView{
    if (_typeView == nil) {
        _typeView = [[ByFootBallTypeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
        _typeView.type = self.type;
        NSArray *itemsAry = @[@"混合投注",@"胜平负",@"让球胜平负",@"总进球",@"比分",@"半全场"];
        _typeView.typeArray = [itemsAry copy];
        __weak typeof(self) weakSelf = self;
        _typeView.indexAtView = ^(NSInteger index){
            [weakSelf.titleBtn setTitle:[itemsAry objectAtIndex:index] forState:UIControlStateNormal];
            [weakSelf maskAction:nil];
            weakSelf.tableView.contentOffset = CGPointMake(0, 0);
            weakSelf.type = index;
            [weakSelf deselectData];
            [weakSelf.tableView reloadData];
        };
    }
    return _typeView;
}

- (UIButton *)maskView{
    if (_maskView == nil) {
        _maskView = [UIButton buttonWithType:UIButtonTypeCustom];
        _maskView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _maskView.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.2];
        [_maskView addTarget:self action:@selector(maskAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskView;
}

- (void)maskAction:(UIButton *)btn{
    
    [_typeView removeFromSuperview];
    _typeView = nil;
    CGAffineTransform  transform = CGAffineTransformRotate(self.titleBtn.imageView.transform,M_PI);
    [UIView beginAnimations:@"1" context:nil];
    [UIView setAnimationDuration:0.5];
    [self.titleBtn.imageView setTransform:transform];
    [UIView commitAnimations];
    [_maskView removeFromSuperview];
    _maskView = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.type = 0;
    [self addLeftBackBarButtonItem];
    [self initViews];
    
    
    [self addBYItemviews:@[@"玩法说明"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self loadOriginalData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData:) name:@"refreshFootBallData" object:nil];
    
    // Do any additional setup after loading the view.
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refreshData:(NSNotification *)sender{
    NSString *str = sender.object;
    NSLog(@"足球数据 = %@", str);
    [self deselectDataWithStr:str];
    [self.tableView reloadData];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];

}

- (void)deselectDataWithStr:(NSString *)str{
    
    [ByFootBallHandle deselectBallDataWithArray:self.spfArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.rspfArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.scoresArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.halfAllArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.goalsArray WithRemainStr:str];
    [self updateBottomState];
}


- (void)deselectData{
    
    [ByFootBallHandle deselectFootBallDataWithArray:self.spfArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.rspfArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.scoresArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.halfAllArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.goalsArray];
    [self updateBottomState];
}



- (void)loadOriginalData{// 0 混合投注 1 胜平负 2.让球胜平负 3 总进球 4 比分 5半全场
   
    self.sectionStateArray = [NSMutableArray arrayWithCapacity:1];
    [self.sectionStateArray removeAllObjects];
   
    [ByCoreToolAlert showLoadind:@"赛事获取中" hasMasker:YES];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByBallApi getFootBallMatch:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        SLog(@"足球赛事 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        if (![dictionary isKindOfClass:[NSDictionary class]]) {
            [ByCoreToolAlert showOnlyText:@"暂无比赛数据"];
            [self.tableView reloadData];
            return ;
        }
        NSArray *timesArray = [dictionary allKeys];
        self.timesArray = [timesArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2 options:NSNumericSearch];
        }];
        NSLog(@"time = %@", self.timesArray);
        NSMutableArray *spfArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *rspfArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *goalsArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *halfAllArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *scoresArray = [NSMutableArray arrayWithCapacity:1];
        
        for (int i = 0; i < self.timesArray.count; i++) {
            [self.sectionStateArray  addObject:@(1)];
            NSString *timeString = self.timesArray[i];
            NSArray *array = dictionary[timeString];
            
            NSMutableArray *spfSectionArray = [NSMutableArray arrayWithCapacity:1];
            NSMutableArray *rspfSectionArray = [NSMutableArray arrayWithCapacity:1];
            NSMutableArray *goalsSectionArray = [NSMutableArray arrayWithCapacity:1];
            NSMutableArray *halfAllSectionArray = [NSMutableArray arrayWithCapacity:1];
            NSMutableArray *scoresSectionArray = [NSMutableArray arrayWithCapacity:1];
            
            
            
            for (NSDictionary *dic in array) {
                if (dic[@"HAD"]) {
                    ByNetModel *HAD_Model = [[ByNetModel alloc] initContentWithDic:dic[@"HAD"]];
                    HAD_Model.timeString = timeString;
                    NSMutableArray *dataArray = [ByFootBallHandle getFinalSpfDataWithDic:dic WithOtherModel:HAD_Model];
                    [spfSectionArray addObject:dataArray];
                }else {
                    NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:3];
                    [spfSectionArray addObject:dataArray];
                }
                
                if (dic[@"HHAD"]) {
                    ByNetModel *HHAD_Model = [[ByNetModel alloc] initContentWithDic:dic[@"HHAD"]];
                     HHAD_Model.timeString = timeString;
                    NSMutableArray *dataArray = [ByFootBallHandle getFinalRspfDataWithDic:dic WithOtherModel:HHAD_Model];
                    [rspfSectionArray addObject:dataArray];
                }else{
                    NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:3];
                    [rspfSectionArray addObject:dataArray];
                }
                
                if (dic[@"TTG"]){
                    ByNetModel *TTG_Model = [[ByNetModel alloc] initContentWithDic:dic[@"TTG"]];
                    TTG_Model.timeString = timeString;
                    NSMutableArray *dataArray = [ByFootBallHandle getFinalGoalsDataWithDic:dic WithOtherModel:TTG_Model];
                    [goalsSectionArray addObject:dataArray];
                    
                }else{
                    NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:8];
                    [goalsSectionArray addObject:dataArray];
                }
                
                
                if (dic[@"HAFU"]) {
                    
                    ByNetModel *HAFU_Model = [[ByNetModel alloc] initContentWithDic:dic[@"HAFU"][@"special_odds"]];
                    HAFU_Model.single = dic[@"HAFU"][@"single"];
                    HAFU_Model.timeString = timeString;
                    NSMutableArray *dataArray = [ByFootBallHandle getFinalHalfAllDataWithDic:dic WithOtherModel:HAFU_Model];
                    [halfAllSectionArray addObject:dataArray];
                }else{
                    NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:9];
                    [halfAllSectionArray addObject:dataArray];
                }
                
                if (dic[@"CRS"]) {
                    ByNetModel *CRS_Model = [[ByNetModel alloc] initContentWithDic:dic[@"CRS"][@"special_odds"]];
                    CRS_Model.single = dic[@"CRS"][@"single"];
                    CRS_Model.timeString = timeString;
                    NSMutableArray *dataArray = [ByFootBallHandle getFinalScoresDataWithDic:dic WithOtherModel:CRS_Model];
                    [scoresSectionArray addObject:dataArray];
                }else{
                    NSArray * titlesArray = @[@"1:0",@"2:0",@"2:1",@"3:0",@"3:1",@"3:2",@"4:0",@"4:1",@"4:2",@"5:0",@"5:1",@"5:2",@"胜其他",@"0:0",@"1:1",@"2:2",@"3:3",@"平其他",@"0:1",@"0:2",@"1:2",@"0:3",@"1:3",@"2:3",@"0:4",@"1:4",@"2:4",@"0:5",@"1:5",@"2:5",@"负其他"];
                    NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:titlesArray.count];
                    [scoresSectionArray addObject:dataArray];
                }
                
                
            }
            
            [spfArray addObject:spfSectionArray];
            [rspfArray addObject:rspfSectionArray];
            [goalsArray addObject:goalsSectionArray];
            [halfAllArray addObject:halfAllSectionArray];
            [scoresArray addObject:scoresSectionArray];
        }
        
        
        //胜平负
        self.spfArray = spfArray;
        //让球胜平负
        self.rspfArray = rspfArray;
        
        //半全场
        self.halfAllArray = halfAllArray;
        //总进球
        self.goalsArray = goalsArray;
        //比分
        self.scoresArray= scoresArray;
        
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}


- (void)rightItemsAction:(NSInteger)rightIndex{
    ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/jiesao?type=3010",BOYE_WEB_BASE_URL]];
    vc.titleString = @"竞彩足球玩法说明";
    [self by_normalPush:vc hasNavi:YES];
}



- (void)initViews{
    
    self.titleBtn = [self createButtonWithFrame:CGRectMake((SCREEN_WIDTH - 130) / 2 - 15, 7, 130, 30) backgroundColor:[UIColor clearColor] title:@"混合过关" titleColor:[UIColor whiteColor] font:FONT(14) target:self action:@selector(titleViewAction:)];
    [self.titleBtn setImage:[UIImage imageNamed:@"mingxifenlei"] forState:UIControlStateNormal];
    self.titleBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 115, 10, 0);
    self.titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 12);
    UINavigationBar * bar = self.navigationController.navigationBar;
    [bar addSubview:self.titleBtn];
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    
    ByFootBallBottomView *bottomView = [[ByFootBallBottomView alloc] init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.tableView.mas_bottom).offset(0);
        if (@available(ios 11.0,*)) {
           make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
           make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.bottomView = bottomView;
    
    bottomView.clearBlock = ^() {
        [self deselectData];
        [self.tableView reloadData];
       
    };
    
    // 0 混合投注 1 胜平负 2.让球胜平负 3 总进球 4 比分 5半全场
    bottomView.sureBlock = ^(){
        
        NSMutableArray *selectedArray1 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.spfArray copy]];
        NSMutableArray *selectedArray2 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.rspfArray copy]];
        NSMutableArray *selectedArray3 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.halfAllArray copy]];
        NSMutableArray *selectedArray4 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.goalsArray copy]];
        NSMutableArray *selectedArray5 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.scoresArray copy]];
        
       
        if (self.numberOfMatch == 1) {
            
             ByNetModel *onlyModel;
            if (selectedArray1.count > 0) {
                onlyModel = [selectedArray1 firstObject];
            }else if (selectedArray2.count > 0){
                onlyModel = [selectedArray2 firstObject];
            }else if (selectedArray3.count > 0){
                onlyModel = [selectedArray3 firstObject];
            }else if (selectedArray4.count > 0){
                onlyModel = [selectedArray4 firstObject];
            }else if (selectedArray5.count > 0){
                onlyModel = [selectedArray5 firstObject];
            }
            
            BOOL value1 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray1];
            BOOL value2 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray2];
            BOOL value3 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray3];
            BOOL value4 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray4];
            BOOL value5 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray5];
            
            if (value1 && value2 && value3 && value4 && value5) {
                
            }else{
                [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                return ;
            }
                
        
            
            NSLog(@"唯一一场比赛 single = %@， title = %@", onlyModel.single,onlyModel.title);
            if (onlyModel.single.integerValue > 0) {
                
                
            }else{
                
                if (self.numberOfMatch < 2) {
                    [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                    return ;
                }
                
            }
            
            
        }else{
            
            if (self.numberOfMatch < 2) {
                [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                return ;
            }
            
            if (self.numberOfMatch > 8) {
                [ByCoreToolAlert showOnlyText:@"最多选择8场比赛"];
                return;
            }
        }
        
      
        NSMutableDictionary *spfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray1];
        NSMutableDictionary *rspfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray2];
        NSMutableDictionary *halfAllDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray3];
        NSMutableDictionary *goalsDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray4];
        NSMutableDictionary *scoresDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray5];
        
        
        NSMutableArray *keysArray = [NSMutableArray arrayWithCapacity:1];
        
        NSArray *spfKeys = [spfDic allKeys];
        NSArray *rspfKeys = [rspfDic allKeys];
        NSArray *halfAllKeys = [halfAllDic allKeys];
        NSArray *goalsKeys = [goalsDic allKeys];
        NSArray *scoresKeys = [scoresDic allKeys];
        
        [keysArray addObjectsFromArray:spfKeys];
        [keysArray addObjectsFromArray:rspfKeys];
        [keysArray addObjectsFromArray:halfAllKeys];
        [keysArray addObjectsFromArray:goalsKeys];
        [keysArray addObjectsFromArray:scoresKeys];
        
        NSSet *set = [NSSet setWithArray:keysArray];
        NSMutableArray *allObjects = [NSMutableArray arrayWithArray:set.allObjects];
        [allObjects sortUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
            return [obj1 compare:obj2];
        }];
        
        NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
        
        for (int i = 0; i < set.count; i++){
            ByNetModel *netModel = [[ByNetModel alloc] init];
           
            NSString *key = allObjects[i];
            if ([rspfDic objectForKey:key]) {
                netModel.rspfArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.rspfArray];
                NSMutableArray *selectRowrspfArray = [rspfDic objectForKey:key];
                netModel.netModel = [[selectRowrspfArray firstObject] copy];
               
            }
            
            if ([spfDic objectForKey:key]) {
                netModel.spfArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.spfArray];
                NSMutableArray *selectRowspfArray = [spfDic objectForKey:key];
                netModel.netModel = [[selectRowspfArray firstObject] copy];
            }
            
            if ([halfAllDic objectForKey:key]) {
                netModel.halfAllArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.halfAllArray];
                NSMutableArray *selectRowHalfAllArray = [halfAllDic objectForKey:key];
                netModel.netModel = [[selectRowHalfAllArray firstObject] copy];
            }
            
            if ([goalsDic objectForKey:key]) {
                netModel.goalsArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.goalsArray];
                NSMutableArray *selectRowGoalsArray = [goalsDic objectForKey:key];
                netModel.netModel = [[selectRowGoalsArray firstObject] copy];
            }
            
            if ([scoresDic objectForKey:key]) {
                netModel.scoresArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.scoresArray];
                NSMutableArray *selectRowScoresArray = [scoresDic objectForKey:key];
                netModel.netModel = [[selectRowScoresArray firstObject] copy];
            }
            [dataArray addObject:netModel];
        }
        
        ByFootBallListVC *vc = [[ByFootBallListVC alloc] init];
        vc.type = self.type;
        vc.dataArray = dataArray;
        [self by_normalPush:vc hasNavi:YES];
    };
}





- (void)titleViewAction:(UIButton *)btn{
    
    if (_typeView) {
        
        [_maskView removeFromSuperview];
        [_typeView removeFromSuperview];
        _maskView = nil;
        _typeView = nil;
        
    }else{
        
        [self.view addSubview:self.maskView];
        [self.view addSubview:self.typeView];
    }
    
   
    CGAffineTransform  transform = CGAffineTransformRotate(self.titleBtn.imageView.transform,M_PI);
    [UIView beginAnimations:@"1" context:nil];
    [UIView setAnimationDuration:0.5];
    [self.titleBtn.imageView setTransform:transform];
    [UIView commitAnimations];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionStateArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSNumber *number = self.sectionStateArray[section];
    NSMutableArray *sectionAry = [self.spfArray objectAtIndex:section];
    if (number.integerValue > 0) {
        return sectionAry.count;
    }else{
        return 0;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // __weak typeof(self) weakSelf = self;
    if (self.type == 0) {
        
        NSMutableArray *sectionAry1 = [self.spfArray objectAtIndex:indexPath.section];
        NSMutableArray *sectionAry2 = [self.rspfArray objectAtIndex:indexPath.section];
        
        NSMutableArray *indexAry1 = [sectionAry1 objectAtIndex:indexPath.row];
        NSMutableArray *indexAry2 = [sectionAry2 objectAtIndex:indexPath.row];
        
        NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
        [dataArray addObjectsFromArray:indexAry1];
        [dataArray addObjectsFromArray:indexAry2];
        
        ByFootBallMixedBetCell *cell = [ByFootBallMixedBetCell cellWithTableView:tableView];
        cell.dataArray = dataArray;
        cell.morePlayTypeBtn.tag = 1000 * indexPath.section + indexPath.row;
        cell.indexAtView = ^(NSInteger index){
        
            ByNetModel *netModel = [dataArray objectAtIndex:index];
            netModel.selected = !netModel.selected;
            [self.tableView reloadData];
            
            [self updateBottomState];
        };
       [self getSelectedCountReloadBtn:cell.morePlayTypeBtn AtIndexPath:indexPath];
        cell.morePlayBlock = ^(){
            
            ByFootBallMixedPopView *popView = [[ByFootBallMixedPopView alloc] init];
            NSMutableArray *halfAllSectionAry = [self.halfAllArray objectAtIndex:indexPath.section];
            NSMutableArray *goalsSectionAry = [self.goalsArray objectAtIndex:indexPath.section];
            NSMutableArray *scoresSectionAry = [self.scoresArray objectAtIndex:indexPath.section];
            
            NSMutableArray *halfAllRowAry = [halfAllSectionAry objectAtIndex:indexPath.row];
            NSMutableArray *goalsRowAry = [goalsSectionAry objectAtIndex:indexPath.row];
            NSMutableArray *scoresRowAry = [scoresSectionAry objectAtIndex:indexPath.row];
            
            [popView setDataSpfArray:dataArray AndHalfAllArray:halfAllRowAry AndGoalsArray:goalsRowAry AndScoresArray:scoresRowAry];
            popView.selectedBlock = ^(NSMutableArray *spfArray, NSMutableArray *halfAllArray, NSMutableArray *goalsArray, NSMutableArray *scoresArray){
                
                NSArray *spfArray1 = [spfArray subarrayWithRange:NSMakeRange(0, 3)];
                NSArray *rspfArray1 = [spfArray subarrayWithRange:NSMakeRange(3, 3)];
                [sectionAry1 replaceObjectAtIndex:indexPath.row withObject:spfArray1];
                [sectionAry2 replaceObjectAtIndex:indexPath.row withObject:rspfArray1];
                [halfAllSectionAry replaceObjectAtIndex:indexPath.row withObject:halfAllArray];
                [goalsSectionAry replaceObjectAtIndex:indexPath.row withObject:goalsArray];
                [scoresSectionAry replaceObjectAtIndex:indexPath.row withObject:scoresArray];
                
                [self.tableView reloadData];
                [self updateBottomState];
            };
            
            popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y-SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
            
            [UIView animateWithDuration:0.5 animations:^{
                popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y+SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
                
            }];
            
        };
        return cell;
        
    }else if (self.type == 1 || self.type == 2){
        
        ByFootBallSPFBetCell *cell = [ByFootBallSPFBetCell cellWithTableView:tableView];
        if (self.type == 1) {
            NSMutableArray *sectionAry = [self.spfArray objectAtIndex:indexPath.section];
            NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
            cell.dataArray = dataArray;
        }else{
            NSMutableArray *sectionAry = [self.rspfArray objectAtIndex:indexPath.section];
            NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
            cell.dataArray = dataArray;
        }
        
        cell.indexAtView = ^(NSInteger index){
            if (self.type == 1) {
                NSMutableArray *sectionAry = [self.spfArray objectAtIndex:indexPath.section];
                NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
                ByNetModel *netModel = [dataArray objectAtIndex:index];
                netModel.selected = !netModel.selected;
            }else{
                NSMutableArray *sectionAry = [self.rspfArray objectAtIndex:indexPath.section];
                NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
                ByNetModel *netModel = [dataArray objectAtIndex:index];
                netModel.selected = !netModel.selected;
            }
           
            [self.tableView reloadData];
            [self updateBottomState];
        };
        return cell;
        
    }else if (self.type == 3){
        
        NSMutableArray *sectionAry = [self.goalsArray objectAtIndex:indexPath.section];
        NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
        
        ByFootBallAllGoalsCell *cell = [ByFootBallAllGoalsCell cellWithTableView:tableView];
        cell.dataArray = dataArray;
       
        cell.indexAtView = ^(NSInteger index){
            ByNetModel *netModel = [dataArray objectAtIndex:index];
            netModel.selected = !netModel.selected;
            [self.tableView reloadData];
             [self updateBottomState];
        };
        return cell;
    }else if (self.type == 4){
       
        NSMutableArray *sectionAry = [self.scoresArray objectAtIndex:indexPath.section];
        NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
        ByFootBallScoreBetCell *cell = [ByFootBallScoreBetCell cellWithTableView:tableView];
        cell.dataArray = dataArray;
        cell.titleView.netModel = [dataArray firstObject];
        ByNetModel *model = [dataArray firstObject];
        cell.titleView.danguanView.hidden = model.single.integerValue == 0;
        cell.scoreBetBlock = ^(){
           
            ByFootBallScoreBetPopView *popView = [[ByFootBallScoreBetPopView alloc] init];
            popView.scoresArray = dataArray;
            popView.selectedBlock = ^(NSMutableArray *scoresArray){
                [sectionAry replaceObjectAtIndex:indexPath.row withObject:scoresArray];
                [self.tableView reloadData];
                [self updateBottomState];
            };
            
            popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y-SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
            
            [UIView animateWithDuration:0.5 animations:^{
                popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y+SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
                
            }];
        };
        return cell;
    }else if (self.type == 5){
        
        NSMutableArray *sectionAry = [self.halfAllArray objectAtIndex:indexPath.section];
        NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
        ByFootBallHalfAllCell *cell = [ByFootBallHalfAllCell cellWithTableView:tableView];
        cell.dataArray = dataArray;
        cell.indexAtView = ^(NSInteger index){
            ByNetModel *netModel = [dataArray objectAtIndex:index];
            netModel.selected = !netModel.selected;
            [self.tableView reloadData];
            [self updateBottomState];
        };
        return cell;
        
    }
    else{
        
        
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        return cell;
    }

   
        
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == 0) {
        return  85 + 81 + 30;
    }else if (self.type == 1 || self.type == 2 || self.type == 3){
        return 85 + 60 + 20;
    }else if (self.type == 4){
        
        NSMutableArray *sectionAry = [self.scoresArray objectAtIndex:indexPath.section];
        NSMutableArray *dataArray = [sectionAry objectAtIndex:indexPath.row];
        NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
        for (ByNetModel *netModel in dataArray){
            if (netModel.selected) {
                [tempArray addObject:netModel.title];
            }
        }
        if (tempArray.count > 0) {
            
          NSString *string = [tempArray componentsJoinedByString:@","];
          CGFloat heigth = [string getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 100, 1000) font:14].size.height + 30;
            if (heigth < 50) {
                heigth = 50;
            }
          return 85 + heigth + 30;
            
        }else{
            
          return 85 + 50 + 30;
            
        }
        
    }else if (self.type == 5){
        return 85 + 100 + 30;
    }
    return 85 + 100 + 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ByFootBallSecitionView *header = [[ByFootBallSecitionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    [header addTarget:self action:@selector(sectionAction:) forControlEvents:UIControlEventTouchUpInside];
    header.tag = 100 + section;
    header.timeLabel.text = [self.timesArray objectAtIndex:section];

    NSMutableArray *sectionAry = [self.spfArray objectAtIndex:section];
    header.countLabel.text = [NSString stringWithFormat:@"共%ld场比赛", sectionAry.count];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:header.countLabel.text];
    //设置颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:BY_THEME_COLOR range:[header.countLabel.text rangeOfString:[NSString stringWithFormat:@"%ld", sectionAry.count]]];
    [attributedString addAttribute:NSFontAttributeName value:B_FONT(16) range:[header.countLabel.text rangeOfString:[NSString stringWithFormat:@"%ld", sectionAry.count]]];
    header.countLabel.attributedText = attributedString;
    
    
    
    return header;
}

- (void)sectionAction:(UIControl *)conrol{
    NSInteger section = conrol.tag-100;
    if([self.sectionStateArray[section] integerValue]==0){
        [self.sectionStateArray replaceObjectAtIndex:section withObject:@(1)];
    }else{
        
        [self.sectionStateArray replaceObjectAtIndex:section withObject:@(0)];
    }
    
    [self.tableView reloadData];
}

- (void)updateBottomState{
    
        NSMutableArray *selectedArray1 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.spfArray copy]];
        NSMutableArray *selectedArray2 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.halfAllArray copy]];
        NSMutableArray *selectedArray3 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.goalsArray copy]];
        NSMutableArray *selectedArray4 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.scoresArray copy]];
        NSMutableArray *selectedArray5 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.rspfArray copy]];
    
       NSMutableDictionary *spfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray1];
       NSMutableDictionary *rspfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray2];
       NSMutableDictionary *halfAllDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray3];
       NSMutableDictionary *goalsDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray4];
       NSMutableDictionary *scoresDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray5];
    
       NSMutableArray *keysArray = [NSMutableArray arrayWithCapacity:1];
    
      NSArray *spfKeys = [spfDic allKeys];
      NSArray *rspfKeys = [rspfDic allKeys];
      NSArray *halfAllKeys = [halfAllDic allKeys];
      NSArray *goalsKeys = [goalsDic allKeys];
      NSArray *scoresKeys = [scoresDic allKeys];
    
       [keysArray addObjectsFromArray:spfKeys];
       [keysArray addObjectsFromArray:rspfKeys];
       [keysArray addObjectsFromArray:halfAllKeys];
       [keysArray addObjectsFromArray:goalsKeys];
       [keysArray addObjectsFromArray:scoresKeys];
    
    NSSet *set = [NSSet setWithArray:keysArray];
    self.numberOfMatch = set.count;
    self.bottomView.countLabel.text = [NSString stringWithFormat:@"已选%ld场比赛", set.count];
}


- (void)getSelectedCountReloadBtn:(UIButton *)btn AtIndexPath:(NSIndexPath *)indexPath{

   
        NSInteger count = 0;
        NSMutableArray *halfAllSectionAry = [self.halfAllArray objectAtIndex:indexPath.section];
        NSMutableArray *goalsSectionAry = [self.goalsArray objectAtIndex:indexPath.section];
        NSMutableArray *scoresSectionAry = [self.scoresArray objectAtIndex:indexPath.section];
        NSMutableArray *spfSectionAry = [self.spfArray objectAtIndex:indexPath.section];
        NSMutableArray *rspfSectionAry = [self.rspfArray objectAtIndex:indexPath.section];
        
        NSMutableArray *halfAllRowAry = [halfAllSectionAry objectAtIndex:indexPath.row];
        NSMutableArray *goalsRowAry = [goalsSectionAry objectAtIndex:indexPath.row];
        NSMutableArray *scoresRowAry = [scoresSectionAry objectAtIndex:indexPath.row];
        NSMutableArray *spfRowAry = [spfSectionAry objectAtIndex:indexPath.row];
        NSMutableArray *rspfRowAry = [rspfSectionAry objectAtIndex:indexPath.row];
        
        NSMutableArray *selectedArray1 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[halfAllRowAry copy]];
        NSMutableArray *selectedArray2 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[goalsRowAry copy]];
        NSMutableArray *selectedArray3 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[scoresRowAry copy]];
        NSMutableArray *selectedArray4 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[spfRowAry copy]];
        NSMutableArray *selectedArray5 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[rspfRowAry copy]];
        count = selectedArray1.count + selectedArray2.count + selectedArray3.count + selectedArray4.count + selectedArray5.count;
        if (count == 0){
            [btn setTitle:@"更多\n玩法" forState:UIControlStateNormal];
        }else{
            [btn setTitle:[NSString stringWithFormat:@"已选\n%ld项", count] forState:UIControlStateNormal];
        }
    
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
